package com.eascs.o2o.reloan.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.o2o.common.vo.ModelResult;
import com.eascs.o2o.recruit.entity.ApplyForInfoVO;
import com.eascs.o2o.recruit.entity.OfficeForInfoVO;
import com.eascs.o2o.reloan.form.ReLoanForm;
import com.eascs.o2o.reloan.service.ReLoanApplyService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;

@RestController
@RequestMapping("/newloan")
public class ReLoanRestController extends BaseRestController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ReLoanApplyService reLoanApplyService;
	
	@RequestMapping("/submitInfo")
	public BaseRestResult<ModelResult<Boolean>> submitInfo(ReLoanForm reLoanForm) {
		ModelResult<Boolean> modelResult=new ModelResult<>();
		try {
			ApplyForInfoVO applyInfo = convertApply(new ApplyForInfoVO(), reLoanForm);
			OfficeForInfoVO officeInfo = convertOffice(new OfficeForInfoVO(), reLoanForm);
			modelResult = reLoanApplyService.applyLoan(applyInfo, officeInfo);
			if (modelResult.getModel()) {
				return BaseRestResult.buildRestResult(modelResult);
			}
		} catch (Exception e) {
			logger.info("ReLoanRestController.submitInfo error[{}]", e.getMessage());
			modelResult.withError("applyLoan error", "申请续贷错误，请联系管理员");
			return BaseRestResult.buildRestResult(modelResult);
		}
		modelResult.withError("applyLoan error", "申请续贷错误，请联系管理员");
		return BaseRestResult.buildRestResult(modelResult);
	}
	
	public static ApplyForInfoVO  convertApply(ApplyForInfoVO applyInfo, ReLoanForm reLoanForm) {
		applyInfo.setUname(reLoanForm.getUname());
		applyInfo.setIdCard(reLoanForm.getIdCard());
		applyInfo.setTelphone(reLoanForm.getTelphone());
		applyInfo.setLoanMoney(reLoanForm.getLoanMoney()*10000);
		applyInfo.setLoanTime(reLoanForm.getLoanTime());
		applyInfo.setLoanType("续贷");
		applyInfo.setCreateTime(new Date());
		return applyInfo;
	}
	public static OfficeForInfoVO  convertOffice(OfficeForInfoVO officeInfo, ReLoanForm reLoanForm) {
		officeInfo.setProvince(reLoanForm.getProvince());
		officeInfo.setCity(reLoanForm.getCity());
		officeInfo.setRegisterTime(reLoanForm.getRegisterTime());
		officeInfo.setOfficeName("");
		officeInfo.setDetailAddress("");
		return officeInfo;
	}
	
}
